package android.textIt.util;

import java.util.List;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.telephony.TelephonyManager;
import android.textIt.backend.dao.UserDao;
import android.textIt.backend.model.User;
import android.util.Log;

public class TextItUtil {
  private final static  String DEBUG_TAG = "TextIt Util";  

	public static String KEY = "TextItPref";
	public static String REGISTRATION_KEY = "registrationKey";
	public static String REGISTRATION_ID_SAVED_KEY = "registrationIdSavedKey";
	public static String OWNER_USERNAME_KEY = "ownerUsernameKey";
	
	public static int TO_INDIVIDUAL = 0;
	public static int TO_GROUP = 1;

  public static String membersToString(List<User> list){
 	 StringBuilder sb = new StringBuilder();
 	 for (User user:list){
 		 sb.append(user.getName() +", ");
 	 }
 	 if (sb.length()>2){
 		 sb.delete(sb.length()-2, sb.length());
 	 }
 	 return sb.toString();
  }
    
  public static List<User> getMyContacts(Context context){
    UserDao userDAO = new UserDao(context);
    List<User> userList = userDAO.getAllUsers();
    User owner = null;
  	for (User user:userList){
  		if (user.getId()==1){
  			owner = user;
  			break;
  		}
  	}
  	if (owner != null){
  		userList.remove(owner);
  	}
  	return userList;
  }
  
	public static void registerC2DM(Context context){
		Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
		registrationIntent.putExtra("app", PendingIntent.getBroadcast(context, 0, new Intent(), 0)); // boilerplate
		registrationIntent.putExtra("sender", "theappmakers@gmail.com");
		context.startService(registrationIntent);
	}
	
	public static void unregisterC2DM(Context context){
		Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
		unregIntent.putExtra("app", PendingIntent.getBroadcast(context, 0, new Intent(), 0));
		context.startService(unregIntent);
	}
	
	public static String getPreferenceByKey(Context context, String key){
    SharedPreferences sharedPreferences =
    	context.getSharedPreferences(TextItUtil.KEY, Context.MODE_PRIVATE);
    return sharedPreferences.getString(key, "");
	}
	
	public static void savePreferenceByKey(Context context, String key, String value){
	  SharedPreferences sharedPreferences = context.getSharedPreferences(TextItUtil.KEY, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
	}

	public static long getMyNumber(Context context){
	  TelephonyManager telMgr =(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	  String mynum =  telMgr.getLine1Number();
	  if (mynum == null){
	  	mynum = "-1";
	  }
	  else{
	  	mynum = mynum.replaceAll("-", "");
	  }
	  Log.v(DEBUG_TAG, "My phone number: " + mynum);
	  return Long.parseLong(mynum);
	}
	
	public static void registerUser(Context context, String name){
		UserDao userDAO = new UserDao(context);
    Long number = getMyNumber(context);
	  User self = new User();
	  self.setName(name);
	  self.setPhone(number);
	  self.setIsTextItUser(1);
	  userDAO.createUser(self);		
    userDAO.registerUser(self);
	
	 self = userDAO.findUserByNumber(number);


	}
}
